﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using System.Linq.Expressions;
using NHibernate.Criterion;
using DAO.IDAL;
namespace DAO.DAL {
	/// <summary>
	/// 接口层D_Manager_role_value
	/// </summary>
	public class D_Manager_role_value:IManager_role_value
	{
	    private ISessionFactory sessionFactory = Manager.SessionFactory;
		#region  成员方法
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(object id)
		{
			using (ISession session = sessionFactory.OpenSession())
            {
                return Get(id) != null;
            }
		}
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public object  Save(Manager_role_value model)
		{
			using (ISession session = sessionFactory.OpenSession())
            {
                var id = session.Save(model);
                session.Flush();
                return id;
            }
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(Manager_role_value model)
		{
			using (var session = sessionFactory.OpenSession())
            {
                session.SaveOrUpdate(model);
                session.Flush();
            }
		}
		/// <summary>
		/// 删除数据
		/// </summary>
		public void Delete(object id)
		{
			using (var session = sessionFactory.OpenSession())
            {
                var customer = session.Load<Manager_role_value>(id);
                session.Delete(customer);
                session.Flush();
            }
		}

		/// <summary>
		/// 删除数据
		/// </summary>
		public void Delete(Manager_role_value model)
		{
			using (var session = sessionFactory.OpenSession())
            {
                session.Delete(model);

                session.Flush();
            }
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Manager_role_value Get(object id)
		{
			using (ISession session = sessionFactory.OpenSession())
            {
                return session.Get<Manager_role_value>(id);
            }
		}
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public IList<Manager_role_value> LoadAll()
		{
			using (ISession session = sessionFactory.OpenSession())
            {
                return session.QueryOver<Manager_role_value>().List();
            }
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public IList<Manager_role_value>  GetList(int? role_id, int? nav_id,int pageIndex = 1, int pageSize = 10)
		{
			using (ISession session = sessionFactory.OpenSession())
            {
                ICriteria crit = session.CreateCriteria(typeof(Manager_role_value));
                if (role_id.HasValue)
                {
                    crit.Add(Restrictions.Eq("role_id", role_id));
                }
                if (nav_id.HasValue)
                {
                    crit.Add(Restrictions.Eq("nav_id", nav_id));
                }
                crit.SetFirstResult((pageIndex-1)*pageSize);
                crit.SetMaxResults(pageSize);
                IList<Manager_role_value> customers = crit.List<Manager_role_value>();
                return customers;
            }
		}

        public int GetCount(int? role_id, int? nav_id) 
        {
            using (ISession session = sessionFactory.OpenSession())
            {
                ICriteria crit = session.CreateCriteria(typeof(Users));
                if (role_id.HasValue)
                {
                    crit.Add(Restrictions.Eq("role_id", role_id));
                }
                if (nav_id.HasValue)
                {
                    crit.Add(Restrictions.Eq("nav_id", nav_id));
                }
                var count = Convert.ToInt32(crit.SetProjection(Projections.RowCount()).UniqueResult());
                return count;
            }
        }
		#endregion  成员方法
	} 
}